﻿using Newtonsoft.Json;
using BuyingSpree_HPV.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace BuyingSpree_HPV
{
    public class ParseUtil
    {
        private static List<Area> areas;
        public static List<Area> GetAreas()
        {
            if (areas == null)
            {
                string jsonfile = AppDomain.CurrentDomain.BaseDirectory + "config//region.json";//JSON文件路径
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                using (StreamReader sr = new StreamReader(jsonfile, Encoding.GetEncoding("GB2312")))
                {
                    string json = sr.ReadToEnd();
                    areas = JsonConvert.DeserializeObject<List<Area>>(json);
                    sr.Close();
                    sr.Dispose();
                }
            }

            return areas;

        }


        public static string[] ParseHeader(string reqHeader)
        {
            if (string.IsNullOrEmpty(reqHeader))
            {
                return null;
            }
            string[] data = new string[2];
            reqHeader = reqHeader.Replace("cookie: ", "Cookie: ");
            int start = reqHeader.IndexOf("tk: ");
            int end = reqHeader.IndexOf("\n", start);
            if (start == -1 || end == -1)
            {
                return null;
            }
            start = start + "tk: ".Length;
            data[0] = reqHeader.Substring(start, end- start);
            start = reqHeader.IndexOf("Cookie: ");
            end = reqHeader.IndexOf("User-Agent");
            if (start == -1 || end == -1)
            {
                return null;
            }
            start = start + "Cookie: ".Length;
            data[1] = reqHeader.Substring(start, end- start);

            return data;
        }

    }
}
